/*
 * 项 目 名:  iCache MSS V002R001
 * 文 件 名:  com.huaweisymantec.icache.mss.device.service.MssDeviceInfoService.java
 * 版    权:  HuaweiSymantec Technologies Co., Ltd. Copyright 2010,  All rights reserved.
 * 描    述:  HuaweiSymantec PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 */
package com.enjoychange.framework.component.storycase.infrastructure.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.enjoychange.core.dao.UniversalDao;
import com.enjoychange.enjoy.common.tool.SQLString;
import com.enjoychange.framework.component.storycase.StoryCase;
import com.enjoychange.framework.component.storycase.infrastructure.IStoryCaseRepository;

/**
 * @author l90003709
 *
 */
@Repository
@Transactional
public class StoryCaseRepository implements IStoryCaseRepository {

	@Override
	public void saveOrUpdate(StoryCase storyCase, boolean isSave) {
		Map<String, Object> paras=new HashMap<String, Object>();
		paras.put("storyid", storyCase.getStoryid());
		paras.put("title", storyCase.getTitle());
		paras.put("specification", storyCase.getSpecification());
		paras.put("newtime",new Date());
		UniversalDao.executeUpdate(isSave?SQLString.STORYCASE_INSERT_SQL:SQLString.STORYCASE_UPDATE_SQL,paras);
	}

	@Override
	public List<StoryCase> queryStoryCase(Map<String, Object> paras) {
		ParameterizedRowMapper<StoryCase> mapper = new ParameterizedRowMapper<StoryCase>() {
			@Override
			public StoryCase mapRow(ResultSet rs, int rowNum) throws SQLException {
				StoryCase storyCase=new StoryCase();
				storyCase.setStoryid(rs.getString("storyid"));
				storyCase.setTitle(rs.getString("title"));
				storyCase.setSpecification(rs.getString("specification"));
				storyCase.setNewtime(rs.getDate("newtime"));
				return storyCase;
			}
		};
		String sql=paras.isEmpty()?SQLString.getNoWhereSql(SQLString.STORYCASE_QUERY_SQL):SQLString.STORYCASE_QUERY_SQL;
		return (List<StoryCase>)UniversalDao.getNamedParameterJdbcTemplate().query(sql, paras, mapper);
		
	}

	@Override
	public void deleteStoryCase(String storyid) {
		Map<String, Object> paras=new HashMap<String, Object>();
		paras.put("storyid", storyid);
		UniversalDao.getNamedParameterJdbcTemplate().update(SQLString.STORYCASE_DELETE_SQL, paras);
	}

}
